import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_repo/chapter02/chapter02.dart';
import 'package:flutter_repo/chapter03/chapter03.dart';
import 'package:flutter_repo/chapter06/chapter06.dart';
import 'package:flutter_repo/widgets/cell.dart';

/// 收集日志
void collectLog(String line) {}

/// 上报日志和异常
void reportErrorAndLog(FlutterErrorDetails details) {}

/// 构建错误信息
FlutterErrorDetails makeDetails(dynamic obj, StackTrace stack) {
  return FlutterErrorDetails(exception: obj, stack: stack);
}

void main() {
  final onError = FlutterError.onError;
  FlutterError.onError = (FlutterErrorDetails details) {
    onError?.call(details);
    reportErrorAndLog(details);
  };
  runZoned(() => runApp(const MyApp()),
      zoneSpecification: ZoneSpecification(
          print: (Zone self, ZoneDelegate parent, Zone zone, String line) {
        collectLog(line);
        parent.print(zone, line);
      }, handleUncaughtError: (Zone self, ZoneDelegate parent, Zone zone,
              Object error, StackTrace stackTrace) {
        final details = makeDetails(error, stackTrace);
        reportErrorAndLog(details);
        parent.print(zone, "${error.toString()} $stackTrace");
      }));
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      routes: {
        // "/": (context) => const MyHomePage(title: "Flutter Demo Home Page"),
        "chapter_02": (context) => const Chapter02(),
        "chapter_03": (context) => const Chapter03(),
        "chapter_06": (context) => const Chapter06(),
      },
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        margin: const EdgeInsets.only(top: 20),
        child: Column(
          children: const [
            Cell(title: "第二章",routeName: 'chapter_02',),
            Cell(title: "第三章", routeName: "chapter_03",),
            Cell(title: "第六章", routeName: "chapter_06",),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Widget SectionWidget(String chapter, List<Widget> children) {
  return Column(
    children: [
      Container(
        decoration: const BoxDecoration(
          color: Colors.black12,
          border: Border(bottom: BorderSide(color: Colors.teal, width: 5)),
        ),
        child: Text(chapter),
      ),
      ...children,
    ],
  );
}
